package com.frxuexi.mymvi

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch


@Composable
fun MyAppPage(navHostController: NavHostController,vm:AppsViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<AppsData>()
    }
    var list2 = remember {
        SnapshotStateList<AppsData>()
    }
    LaunchedEffect(Unit){
        launch {
            vm.state.collect{
                when(it){
                    is AppsState.onSuccess->{
                        Log.i("===",it.data.toString())
                        list.addAll(it.data)
                        list2.addAll(it.data)
                    }
                    is AppsState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }

        launch {
            //发送意图
            vm.channel.send(AppsIntent.getApps)
        }
    }
    var search by remember {
        mutableStateOf("")
    }
    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp)) {
        TextField(value = search, onValueChange = {search=it
                list.clear()
                list.addAll(list2.filter {
                    it.appName.contains(search)
                })
                                                  },
            label = { Text(text = "请输入搜索内容")},
            leadingIcon = { Icon(imageVector = Icons.Default.Search, contentDescription = "")},
            trailingIcon = { Icon(imageVector = Icons.Default.Call, contentDescription = "")},
            modifier = Modifier.fillMaxWidth().border(1.dp,Color.DarkGray, RoundedCornerShape(30.dp)).clip(RoundedCornerShape(30.dp)),
            colors= TextFieldDefaults.colors(
                focusedContainerColor= Color.White,
                unfocusedContainerColor= Color.White,
                focusedIndicatorColor= Color.White,
                unfocusedIndicatorColor=Color.White
            ))
        LazyVerticalGrid(modifier = Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp), columns=GridCells.Fixed(3),content = {
            items(list){
                Column(horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.clickable {
                        val json = Gson().toJson(it)
                        MMKV.defaultMMKV().encode("detail",json)
                        navHostController.navigate("detail")
                    }) {
                    AsyncImage(model = it.appIcon, contentDescription = "", modifier = Modifier.size(40.dp))
                    Spacer(modifier = Modifier.height(5.dp))
                    Text(text = it.appName)
                }
            }
        })
    }

}